package com.bum.rss.site;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.InitializingBean;

import com.bum.rss.exp.om.RssEntry;
import com.bum.rss.site.cat.MuctimCat;
import com.bum.rss.site.util.BaseParser;

public class MuctimSite extends Site implements MuctimCat, InitializingBean {
	final HashMap<String, String>	CAT_CODES	= new HashMap<String, String>();
	final List<RssEntry>			CATEGORIES	= new ArrayList<RssEntry>(CATS.length);

	@Override
	public void afterPropertiesSet() throws Exception {
		for (int n = CATS.length, i = 0; i < n; i++) {
			CAT_CODES.put(CATS[i][0], CATS[i][2]);
			CATEGORIES.add(new RssEntry(getCode() + URL_SEPARATOR + CATS[i][0], CATS[i][1]));
		}
	}

	@Override
	public List<RssEntry> listSiteCats(String user) {
		return CATEGORIES;
	}

	@Override
	public List<RssEntry> listCatEntrys(String cat, String user) {
		int page = 1;
		if (cat.indexOf(PAGE_SEPARATOR) > 0) {
			String[] parts = StringUtils.split(cat, PAGE_SEPARATOR);
			page = NumberUtils.toInt(parts[1], 1);

			cat = parts[0];
		}

		String url = "http://news.muctim.com.vn/list/" + CAT_CODES.get(cat) + "_1_a.html?page=" + page;
		String html = BaseParser.loadHtml(url);
		html = BaseParser.parseContent(html, "<div class=\"scol1\">", "</ul> </div> <div class=\"tc mt10\">");

		if (StringUtils.isBlank(html)) {
			return null;
		}

		List<RssEntry> entries = new LinkedList<RssEntry>();

		html = html.replace(".html\"><img width", "<img width");
		String[] items = html.split("<li class=\"clearfix\">");
		for (int i = 1; i < items.length; i++) {
			String item = items[i];

			String link = BaseParser.parseContent(item, "<p><a href=\"", "\">");
			if (StringUtils.isBlank(link)) {
				continue;
			}
			link = "http://news.muctim.com.vn" + link;
			link = getRssLink(link, cat);

			String title = BaseParser.parseContent(item, ".html\">", "</a>");

			String intro = BaseParser.parseContent(item, "</a></p> ", "<a class=\"more");
			intro = BaseParser.removeHtmlTag(intro);

			String imgSrc = BaseParser.parseContent(item, "<img width=\"130\" src=\"", "\" ");

			RssEntry entry = new RssEntry();
			entry.setTitle(title);
			entry.setUrl(link);
			entry.setIntro(intro);
			entry.setThumb(imgSrc);

			entries.add(entry);
		}

		if (entries.size() >= 10) {
			entries.add(new RssEntry(getCode() + URL_SEPARATOR + cat + PAGE_SEPARATOR + (page + 1), NEXT_PAGE_TITLE));
		}
		if (page > 1) {
			entries.add(0, new RssEntry(getCode() + URL_SEPARATOR + cat + PAGE_SEPARATOR + (page - 1), PREV_PAGE_TITLE));
		}

		return entries;
	}

	@Override
	public String getRssLink(String httpLink) {
		return getRssLink(httpLink, "m");
	}

	// http://news.muctim.com.vn/view/47618_Khoa_tinh_yeu_tren_cau_Hung_Vuong_MT_1148.html => rss://muctim/m/47618/1148
	protected String getRssLink(String httpLink, String catId) {
		if (StringUtils.isBlank(httpLink)) {
			return null;
		}

		String part0;
		part0 = BaseParser.parseContent(httpLink, "/view/", "_");

		if (part0 == null) {
			return null;
		}
		return getCode() + URL_SEPARATOR + catId + URL_SEPARATOR + part0;
	}

	static String[]	REP_FROM	= {
			"http://www.boyvn.com/home/images/stories/bao/", "http://antg.cand.com.vn/Images/reddot.gif",
			"http://www.boyvn.com/home/images/stories/2013/thang_11/4.gif"
								};
	static String[]	REP_TO		= {
			"", "", ""
								};

	@Override
	public RssEntry loadEntry(String path, String user) {
		String[] parts = path.split("/");
		String url = "http://news.muctim.com.vn/view/" + parts[0] + "_a.html";
		String html = BaseParser.loadHtml(url);

		html = BaseParser.parseContent(html, "<!-- BOX CONTENT -->", "<span class=\"printer\">");
		String title = BaseParser.parseContent(html, "<span>", "</span>");
		html = BaseParser.parseContent(html, "<div class=\"post-detail\"> <div>", "</div> </div> <div> Lượt xem: ");

		if (title == null || html == null) {
			return null;
		}

		RssEntry entry = new RssEntry();
		entry.setTitle(title);
		entry.setContent(html);
		return entry;
	}

	public static void main(String[] args) throws Exception {
		MuctimSite site = new MuctimSite();
		site.afterPropertiesSet();

		RssEntry detail = site.loadEntry("48174", null);
		System.out.println(detail.getTitle());
		System.out.println(detail.getContent());

//		List<RssEntry> list = site.listCatEntrys("1", null);
//		if (list != null) {
//			for (RssEntry entry : list) {
//				System.out.println(entry.getUrl() + "\n" + entry.getTitle() + "\n" + entry.getThumb() + "\n" + entry.getIntro() + "\n");
//			}
//		}
	}
}
